﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace EasySL.Controls
{
    public class ExDataTable : ObservableCollection<DataRow>
    {
        public Action<DataRow> OnChecked { get; set; }
        
        public void Fill(IEnumerable items)
        {
            this.Clear();

            foreach (object item in items)
            {
                DataRow row = new DataRow();
                row.Fill(item);
                this.Add(row);
            }
        }

        public void InitCheckState(DataRow row)
        {
            row["IsChecked"] = false;

            row.OnChecked += r =>
            {
                if (this.OnChecked != null)
                    this.OnChecked(r);
            };
        }

        public void InitCheckState()
        {
            foreach (var row in this)
                InitCheckState(row);
        }

        public void FillOrder(string property)
        {
            string orderProperty = property + "_Order";
            var orderItems = this.OrderByDescending(r => r[property]).ToList();
            for (int i = 0; i < orderItems.Count(); i++)
                orderItems[i][orderProperty] = i + 1;
        }
    }

    /// <summary>
    /// Data row
    /// </summary>
    public class DataRow : INotifyPropertyChanged
    {
        private Dictionary<string, object> _data = new Dictionary<string, object>();

        public Action<DataRow> OnChecked { get; set; }

        public object this[string index]
        {
            get
            {
                if (_data.ContainsKey(index))
                    return _data[index];
                else
                    return null;
            }
            set 
            { 
                _data[index] = value;
            }
        }

        public Dictionary<string, object> Data
        {
            get
            {
                return _data;
            }
        }

        public bool IsChecked
        {
            get
            {
                return (bool)this["IsChecked"];
            }
            set
            {
                this["IsChecked"] = value;
                NotifyPropertyChanged("IsChecked");

                if (this.OnChecked != null)
                    this.OnChecked(this);
            }
        }

        public void Fill(object item)
        {
            Type type = item.GetType();
            
            foreach (var propertyInfo in type.GetProperties())
                this[propertyInfo.Name] = propertyInfo.GetValue(item, null);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
